// pages/tools/tools.js
let {GetStepNum,GetOpenid,GetToolData,updateToolData}=require("../../api/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leanToolsData:[
      {
        title:"1分钟了解自己",
        url:"../understandMy/understandMy",
        img:"../../image/toolsImg/lean_icon.png"
      },
      {
        title:"身体质量指数",
        url:"../bodyBmi/bodyBmi",
        img:"../../image/toolsImg/lean_icon2.png"
      },
      {
        title:"基础代谢计算",
        url:"../basicMetabolism/basicMetabolism",
        img:"../../image/toolsImg/lean_icon3.png"
      },
      {
        title:"食物热量查询",
        url:"../foodEnergy/foodEnergy",
        img:"../../image/toolsImg/lean_icon4.png"
      },
      {
        title:"食物GI指数查询",
        url:"../foodGi/foodGi",
        img:"../../image/toolsImg/lean_icon5.png"
      },
      {
        title:"食物能量互换表",
        url:"../foodPower/foodPower",
        img:"../../image/toolsImg/lean_icon6.png"
      },
    ],
    //腰围
    waistlineCurrentValue:"",
    waistlineValue:"",
    isShowWaistline:false,
    //目标体重
    targetCurrentValue:"",
    targetValue:"",
    isShowTarget:false,
    //当前体重
    nowCurrentValue:"",
    nowValue:"",
    isShowNow:false,
    stepNumValue:"",
  },
  
  // 腰围
  showWaistline(){
    this.setData({
      isShowWaistline:true,
    })

  },
  hiddenWaistline(){
    this.setData({
      isShowWaistline:false
    })
  },
  changeWaistline(e){
    const val = e.detail.value
    this.setData({
      waistlineCurrentValue:val
    })
  },
  saveWaistline(){
    if(!this.data.waistlineCurrentValue){
      wx.showToast({

        title: '值不能为空',
   
        icon: 'none',
   
        duration: 2000//持续的时间
   
      })
      return;
    }
    this.setData({
      waistlineValue:this.data.waistlineCurrentValue
    })
    this.hiddenWaistline();
    this.saveData();
  },
  // 目标体重
  showTarget(){
    this.setData({
      isShowTarget:true,
    })

  },
  hiddenTarget(){
    this.setData({
      isShowTarget:false
    })
  },
  changeTarget(e){
    const val = e.detail.value
    this.setData({
      targetCurrentValue:val
    })
  },
  saveTarget(){
    if(!this.data.targetCurrentValue){
      wx.showToast({

        title: '值不能为空',
   
        icon: 'none',
   
        duration: 2000//持续的时间
   
      })
      return;
    }
    this.setData({
      targetValue:this.data.targetCurrentValue
    })
    this.saveData();
    this.hiddenTarget();
  },
  // 当前体重
  showNow(){
    this.setData({
      isShowNow:true,
    })

  },
  hiddenNow(){
    this.setData({
      isShowNow:false
    })
  },
  changeNow(e){
    const val = e.detail.value
    this.setData({
      nowCurrentValue:val
    })
  },
  saveNow(){
    if(!this.data.nowCurrentValue){
      wx.showToast({

        title: '值不能为空',
   
        icon: 'none',
   
        duration: 2000//持续的时间
   
      })
      return;
    }
    this.setData({
      nowValue:this.data.nowCurrentValue
    })
    this.saveData();
    this.hiddenNow();
  },
  //获取步数
  getRun(){
    let that=this;
    if (this.data.stepNumValue!="") {
        return;
    }
        wx.login({
          success: function (res) {
            GetOpenid(res.code,(e)=>{
              let session_key=e.data.session_key;
                    wx.getWeRunData({
                      success(resRun) {
                        let encryptedData=resRun.encryptedData;
                        let iv=resRun.iv;
                        GetStepNum(session_key,encryptedData,iv,(e)=>{
                          that.setData({
                            stepNumValue:e.data.stepInfoList.pop().step
                          })
                        });
                      }
                    })
            })
          }
        });

  },
  // getSessionKey(){
  //   // GetOpenid()
  // },
  // 保存数据
  saveData(){
    let {waistlineValue,targetValue,nowValue}=this.data;
    let openid=wx.getStorageSync("openid");
    if (openid) {
      let data={
        openid:wx.getStorageSync("openid"),
        waistlineValue,
        targetValue,
        nowValue
      }
      console.log(data);
      // console.log()
      updateToolData(data,(e)=>{
        console.log(e);
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      //   wx.checkSession({
      //     success () {
      //       console.log("未过期");
            
      //     },
      //     fail () {
      //       console.log("过期了");

      //       // session_key 已经失效，需要重新执行登录流程
      //     }
      // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let openid=wx.getStorageSync("openid");
    if (openid) {
      GetToolData(openid,(e)=>{
        // console.log(e.data);
        this.setData({
          waistlineValue:e.data.WaistLine,
          waistlineCurrentValue:e.data.WaistLine,
          targetValue:e.data.TargetWeight,
          targetCurrentValue:e.data.TargetWeight,
          nowValue:e.data.CurrentWeight,
          nowCurrentValue:e.data.CurrentWeight,
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})